package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author msjykf
 * 微信客户信息校验接口
 *
 *  
 *
 */

public class wxInterfacePools implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(wxInterfacePools.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			PropertiesUtil pt = new PropertiesUtil();
			def device = request.getParameter("device");
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			def enableSSLflag = 0;
			def url = "http://"+pt.readValue("dmzip")+"/EliteGeneralHttpTask/GeneralService/Access_Token?code="+request.getParameter("code"); 
			def wxmsg = Util.doHttpGet(url,charset,timeout,enableSSLflag);
			JSONObject o = JSONObject.fromObject(wxmsg);
			def openid= o.get("openid");
			if(openid.equals("")) {
				return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"openid不能为空！\"}";
			};
			def smap = eliteBean.chaxun_sql("select nickname,credentno,is_bind from wx_client@dblink_hs_wx where open_id='"+openid+"'","dataSource");
			if(smap.size()==0) {
				return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"未订阅客户！\"}";
			};
			def nickname = Util.filterForObject(smap[0].get("nickname")); //微信昵称
			def idno = Util.filterForObject(smap[0].get("credentno")); //证件号码
			def isbind = Util.filterForObject(smap[0].get("is_bind")); //是否绑定微信
			if(idno.equals("")) {
				return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"未绑定微信客户！\"}";
			}else if(idno.length() == 15) {
				idno = get18Ic(idno);
			};
			url = pt.readValue("crmip")+"CustInfoDeal";
			def jsonbody = "{\"sk_invpty\":\"\",\"invpty_name\":\"\",\"cust_type\":\"\",\"fundacco\":\"\",\"std_idno\":\""+idno+"\",\"mobile\":\"\",\"tradeaccount\":\"\"}";
			def returnbody = Util.doHttpPost(url,jsonbody,charset,timeout,enableSSLflag);
			JSONObject obj = JSONObject.fromObject(returnbody);
			def code = obj.get("code");
			if(code==1) {
				eliteBean.update("update customer set webchat_nickname='"+nickname+"',stringfield6='"+isbind+"' where customer_guid='"+obj.get("customer_guid")+"'","dataSource");
				return "{\"state\" :\"1\" , \"success\" : true,\"result\" :{\"loginName\" :\""+obj.get("sk_invpty")+"\",\"password\" :\"\"} ,\"msg\" :\"接口调用成功\"}";
			}else {
				return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"查询不到客户信息！\"}";
			}
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"state\" :\"0\" , \"success\" : false,\"result\" :{} ,\"msg\" :\"接口处理出错！\"}";
		}
	}
	public static String get18Ic(String identityCard) {
		String retId = "";
		String id17 = "";
		int sum = 0;
		int y = 0;
		// 定义数组存放加权因子（weight factor）
		int[] wf = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ] as int[];
		
		// 定义数组存放校验码（check code）
		String[] cc = [ "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" ] as String[];
		if (identityCard.length() != 15) {
			return identityCard;
		}
		// 加上两位年19
		id17 = identityCard.substring(0, 6) + "19" + identityCard.substring(6);
		// 十七位数字本体码加权求和
		for (int i = 0; i < 17; i++) {
			sum = sum + Integer.valueOf(id17.substring(i, i + 1)) * wf[i];
		}
		// 计算模
		y = sum % 11;
		// 通过模得到对应的校验码 cc[y]
		retId = id17 + cc[y];
		return retId;
	}
}